import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { RechargeOrderApi } from '#/api/reservation/rechargeorder';

import { useAccess } from '@vben/access';

import { DICT_TYPE, getDictOptions, getRangePickerDefaultProps } from '#/utils';

const { hasAccessByCodes } = useAccess();

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'memberId',
      label: '会员id',
      component: 'Input',
      componentProps: {
        placeholder: '请输入会员id',
      },
    },
    {
      fieldName: 'activityId',
      label: '活动id',
      component: 'Input',
      componentProps: {
        placeholder: '请输入活动id',
      },
    },
    {
      fieldName: 'amount',
      label: '充值价格',
      component: 'Input',
      componentProps: {
        placeholder: '请输入充值价格',
      },
    },
    {
      fieldName: 'giftAmount',
      label: '赠送金额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入赠送金额',
      },
    },
    {
      fieldName: 'paymentMethod',
      label: '支付方式',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_PAYMENT_METHOD, 'number'),
        placeholder: '请选择支付方式',
      },
    },
    {
      fieldName: 'completeTime',
      label: '完成时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'ipAddress',
      label: '用户ip地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入用户ip地址',
      },
    },
    {
      fieldName: 'status',
      label: '服务状态',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.MRS_PAYMENT_STATUS, 'number'),
        placeholder: '请选择服务状态',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'memberId',
      label: '会员id',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入会员id',
      },
    },
    {
      fieldName: 'activityId',
      label: '活动id',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入活动id',
      },
    },
    {
      fieldName: 'paymentMethod',
      label: '支付方式',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_PAYMENT_METHOD, 'number'),
        placeholder: '请选择支付方式',
      },
    },
    {
      fieldName: 'completeTime',
      label: '完成时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '服务状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MRS_PAYMENT_STATUS, 'number'),
        placeholder: '请选择服务状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  onActionClick?: OnActionClickFn<RechargeOrderApi.RechargeOrder>,
): VxeTableGridOptions<RechargeOrderApi.RechargeOrder>['columns'] {
  return [
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
    },
    {
      field: 'memberId',
      title: '会员id',
      minWidth: 120,
    },
    {
      field: 'activityId',
      title: '活动id',
      minWidth: 120,
    },
    {
      field: 'amount',
      title: '充值价格',
      minWidth: 120,
    },
    {
      field: 'giftAmount',
      title: '赠送金额',
      minWidth: 120,
    },
    {
      field: 'paymentMethod',
      title: '支付方式',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_PAYMENT_METHOD },
      },
    },
    {
      field: 'completeTime',
      title: '完成时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'ipAddress',
      title: '用户ip地址',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '服务状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MRS_PAYMENT_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'operation',
      title: '操作',
      minWidth: 200,
      align: 'center',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      cellRender: {
        attrs: {
          nameField: 'id',
          nameTitle: '充值订单',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['reservation:recharge-order:update']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['reservation:recharge-order:delete']),
          },
        ],
      },
    },
  ];
}
